package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.InviteInfo;
import com.xindaoapp.happypet.protocol.MoccaApi;
import com.xindaoapp.happypet.ui.BaseActivity;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.share.SharePopupWindow;

/**
 * 我的--个人中心--邀请好友
 * 
 * @author Yangqing
 * 
 */
public class InviteActivity extends BaseActivity {

    private TextView tv_info_1, tv_requestCode, tv_invitetip, tv_ranking, tv_look;
    private Button btn_yaoqing;

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_invite;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTopBarTitle("邀请好友");
        // 成功邀请人数信息
        tv_info_1 = (TextView) findViewById(R.id.tv_info_1);
        // 排名
        tv_ranking = (TextView) findViewById(R.id.tv_ranking);
        // 查看邀请的好友
        tv_look = (TextView) findViewById(R.id.tv_look);
        // 唯一邀请码
        tv_requestCode = (TextView) findViewById(R.id.tv_requestCode);
        // 邀请描述
        tv_invitetip = (TextView) findViewById(R.id.tv_invitetip);
        // 邀请好友
        btn_yaoqing = (Button) findViewById(R.id.btn_yaoqing);

        tv_requestCode.setText(UserState.getUserUid());

    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getInviteInfo(new IRequest<InviteInfo>() {
            @Override
            public void request(InviteInfo t) {
                if (t != null) {
                    onDataArrived(true);

                    String text = getResources().getString(R.string.invite_activity);
                    tv_info_1.setText(Html.fromHtml(String.format(text, t.invitecount,
                            t.invitereward)));
                    tv_ranking.setText(Html.fromHtml(String.format(
                            getResources().getString(R.string.ranking_activity), t.topnum)));
                    tv_invitetip.setText(t.invitetip);
                } else {
                    onDataArrived(false);
                }
            }
        });
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        btn_yaoqing.setOnClickListener(mOnClickListener);
        tv_info_1.setOnClickListener(mOnClickListener);
        tv_look.setOnClickListener(mOnClickListener);
    }

    private final OnClickListener mOnClickListener = new OnClickListener() {
        private SharePopupWindow win;

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_yaoqing:// 点击邀请
                    // TODO 分享
                    if (UserState.isLogged(InviteActivity.this)) {
                        win =
                                new SharePopupWindow(InviteActivity.this,
                                        CommonParameter.UserState.getUser().myqrcode, false);
                        CommonUtil.addScreenBg(win, InviteActivity.this);
                        win.setDefaultIconId(R.drawable.icon);
                        win.setPostCome(MoccaApi.VALUE_COME_PERSONCENTER);

                        String sinaContent =
                                String.format(
                                        "#尾巴圈宠物#我正在玩尾巴圈，超好玩，注册时填写我的邀请码：%1$s，有惊喜哦！@尾巴圈 ，我的养宠神器！下载地址>>%2$s",
                                        UserState.getUserUid(),
                                        getResources().getString(R.string.app_download_url));
                        String WXContent =
                                String.format("注册尾巴圈，填写邀请码：%s，有惊喜哦！", UserState.getUserUid());
                        String QQContent =
                                String.format("注册尾巴圈，填写邀请码：%s，有惊喜哦！", UserState.getUserUid());

                        win.setInfors(InviteActivity.this, sinaContent, "尾巴圈，我的养宠神器", sinaContent,
                                WXContent, QQContent, CommonParameter.UserState.getUser().myurl)
                                .showAtLocation(findViewById(R.id.bottomline), Gravity.BOTTOM, 0, 0);
                        // win.setInfors(
                        // InviteActivity.this,
                        // String.format(
                        // "#尾巴圈宠物# 我正在玩尾巴圈，超好玩，注册时填写我的邀请码：%1$s，有惊喜哦！尾巴圈，我的养宠神器！下载地址：%2$s（来自@尾巴圈）",
                        // UserState.getUserUid(),
                        // CommonParameter.UserState.getUser().myurl), "尾巴圈，我的养宠神器 !",
                        // String.format("注册尾巴圈，填写邀请码：%s，有惊喜哦！", UserState.getUserUid()),
                        // CommonParameter.UserState.getUser().myurl).showAtLocation(
                        // findViewById(R.id.bottomline), Gravity.BOTTOM, 0, 0);
                    }
                    break;
                case R.id.tv_info_1:// 邀请好友

                    break;
                case R.id.tv_look:
                    startActivity(new Intent(InviteActivity.this, RequestFriendActivity.class));
                    break;
            }
        }
    };

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    };

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                InviteActivity.this.finish();
            }
        };
    }
}
